package com.ruoyi.category.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.category.domain.SysPropTerrain;
import com.ruoyi.category.mapper.SysPropTerrainMapper;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.category.mapper.SysPorpMapper;
import com.ruoyi.category.domain.SysPorp;
import com.ruoyi.category.service.ISysPorpService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 物品信息Service业务层处理
 * 
 * @author yjf
 * @date 2022-07-07
 */
@Service
public class SysPorpServiceImpl implements ISysPorpService 
{
    @Autowired
    private SysPorpMapper sysPorpMapper;

    @Autowired
    private SysPropTerrainMapper propTerrainMapper;

    /**
     * 查询物品信息
     * 
     * @param propId 物品信息主键
     * @return 物品信息
     */
    @Override
    public SysPorp selectSysPorpByPropId(Long propId)
    {
        SysPorp porpInfo=sysPorpMapper.selectSysPorpByPropId(propId);

        SysPropTerrain sysPropTerrain = new SysPropTerrain();
        sysPropTerrain.setPropId(propId);
        List<SysPropTerrain> propTerrain = propTerrainMapper.selectPropTerrain(sysPropTerrain);
        List<Long> terrainIds = propTerrain.stream().map(s->s.getTerrainId()).collect(Collectors.toList());
        porpInfo.setTerrainIds(terrainIds);
        return porpInfo;
    }

    /**
     * 查询物品信息列表
     * 
     * @param sysPorp 物品信息
     * @return 物品信息
     */
    @Override
    public List<SysPorp> selectSysPorpList(SysPorp sysPorp)
    {
        return sysPorpMapper.selectSysPorpList(sysPorp);
    }

    /**
     * 新增物品信息
     * 
     * @param sysPorp 物品信息
     * @return 结果
     */
    @Override
    @Transactional
    public int insertSysPorp(SysPorp sysPorp)
    {
        //新增物品
        sysPorp.setCreateTime(DateUtils.getNowDate());
        Integer num = sysPorpMapper.insertSysPorp(sysPorp);
        //新增物品-地图关联
        insertSysPropTerrain(sysPorp);
        return num;
    }

    /**
     * 修改物品信息
     * 
     * @param sysPorp 物品信息
     * @return 结果
     */
    @Override
    @Transactional
    public int updateSysPorp(SysPorp sysPorp)
    {
        //新增物品-地图关联
        insertSysPropTerrain(sysPorp);
        //修改物品信息
        sysPorp.setUpdateTime(DateUtils.getNowDate());
        return sysPorpMapper.updateSysPorp(sysPorp);
    }

    /**
     * 新增物品地图关系
     * @param sysPorp 物品信息
     * @return 结果
     */
    public int insertSysPropTerrain(SysPorp sysPorp)
    {
        //删除物品地图关联
        propTerrainMapper.deletePropTerrainByPropId(sysPorp.getPropId());
        //插入物品地图关联
        List<SysPropTerrain> propTerrainList =new ArrayList<>();
        for (Long terrainId : sysPorp.getTerrainIds()) {
            SysPropTerrain sysPropTerrain = new SysPropTerrain();
            sysPropTerrain.setTerrainId(terrainId);
            sysPropTerrain.setPropId(sysPorp.getPropId());
            propTerrainList.add(sysPropTerrain);
        }
        if (propTerrainList.size() < 1)
        {
            throw new ServiceException("地图选项不能为空！");
        }
        return propTerrainMapper.batchPropTerrain(propTerrainList);
    }
    
    /**
     * 批量删除物品信息
     * 
     * @param propIds 需要删除的物品信息主键
     * @return 结果
     */
    @Override
    public int deleteSysPorpByPropIds(Long[] propIds)
    {
        return sysPorpMapper.deleteSysPorpByPropIds(propIds);
    }
}
